<?
/*****************************************************************************
 * 文件名：VersionControl.php
 * 作用：VersionControl控制器类 负责词库管理系统给
 * 创建时间：2010.1.19
 * 作者：赵鹏城
 * ****************************************************************************/
?>

<?php

class Controller_VersionControl extends FLEA_Controller_Action
{
    /*数据表对象*/
    var $version_control_tdg;
    var $word_type_tdg;
    var $word_bank_tdg;

    /*构造函数*/
    function Controller_VersionControl() {
        $this->version_control_tdg = & FLEA::getSingleton('Model_VersionControl');
        $this->word_type_tdg = & FLEA::getSingleton('Model_WordType');
        $this->word_bank_tdg = & FLEA::getSingleton('Model_WordBank');
    }
    
    /*
     * 默认action
     */
    function actionIndex() {
        $rowset = $this->version_control_tdg->findAll();
        include("VersionControl.php");
    }
    
    function actionPublic() {
        $time_start = microtime_float();
        if (empty($_POST['build_bin'])) {
            //echo "no bin";
        } else {
            //echo "bin";
        }
        $typeset = $this->word_type_tdg->findAll();
        //dump($typeset);
        //echo dirname("index.php");
        //echo __FILE__;
        //echo getcwd();
        //echo $_SERVER[PHP_SELF];
        //$dbo = $this->version_control_tdg->getDBO();
        //$id = $dbo->_insertId();
        $newdir = "version-".strtotime ("now");
        $version_data_dir = "public_data";
        $path = getcwd()."/".$version_data_dir."/".$newdir; //linux
        mkdir($path);
        FLEA::loadClass('FLEA_Helper_Pager');
        $sum = 0;
        foreach ($typeset as $type) {
            $phrase_path = $path."/phrase-terms-".$type['type'].".chs"; //linux
            $fp = fopen($phrase_path, 'x');
            $page = 0;
            $pageSize = 10000;
            $conditions = "type=\"".$type['type']."\" and isgood=1";
            $sort = 'id ASC';
            while (1) {
                //利用分页机制 逐批次取数据
                $pager = new FLEA_Helper_Pager($this->word_bank_tdg, $page, $pageSize, $conditions, $sort);
                $phraseset = $pager->findAll();
                $PagerData = $pager->getPagerData();
                $somecontent = "";
                foreach ($phraseset as $phrase) {
                    $somecontent = $somecontent.$phrase['word']."\n";
                }
                if (fwrite($fp, $somecontent) === FALSE) {
                    echo "不能写入到文件 $phrase_path";
                    fclose($fp);
                    exit;
                }
                if ($page == $PagerData['lastPage']) {
                    break;
                }
                $page = $PagerData['nextPage'];
            }
            $sum += $PagerData['totalCount'];
            fclose($fp);
        }
        
        $new_version_row = array (
            'version_name' => $newdir,
            'href' => "/".$version_data_dir."/".$newdir,
            'sum' => $sum,
        );
        $this->version_control_tdg->create($new_version_row);
        echo "词库发布成功";
        echo "<a href='/".$version_data_dir."/".$newdir."'>浏览文件</a>"; //linux
        echo "&nbsp";
        echo "<a href='".url('VersionControl')."'>返回</a>";
        $time_end = microtime_float();
        $time = $time_end - $time_start;
        echo "(用时 $time 秒)";
    }
    
    function actionDeleteVersion() {
        $this->version_control_tdg->remove($_GET);
         redirect(url('VersionControl'));
    }
    
    function microtime_float() {
        list($usec, $sec) = explode(" ", microtime());
        return ((float)$usec + (float)$sec);
    }

}

?>
